"use strict";

import axios ,{AJAX_START,AJAX_SUCCESS,AJAX_ERROR} from  "../../lib/networkAxios";
import * as type from "../mutation-types";

export const state = {
    visit:{
      path:"/"
    },
    user:{
        isAxiosing: false,
        info:{
        }
    }
};

export const getters = {
    userInfo: state => state.user.info,
    visitPath: state => state.visit.path,

};

const getUserInfoPost=({commit},user={})=>{
    commit(type.GETUSERINFO_POST,user)
};

export const actions = {
    getLoinInfo ({commit}) {
        getUserInfoPost({commit},{status:AJAX_START})
        return axios.post(`${__SERVER_HOST__}/AdminApi/GetUserInfo`).then(res=>{
            getUserInfoPost({commit},{status:AJAX_SUCCESS,info:res.data,msg:res.msg})
            return  Promise.resolve(res)
        }).catch(res=>{
            getUserInfoPost({commit},{status:AJAX_ERROR,msg:res.msg})
            return  Promise.reject(res)
        })
    },
    setVisitPath({commit},path){
        commit(type.SETVISITPATH ,path)
    }
};


//--------------------
//Mutations Handle
//--------------------

const getLoinInfoPostMutationHandle =(state,action)=>{
    switch (action.status){
        case AJAX_START:
            return Object.assign( state, {
                isAxiosing: true,
            });
        case AJAX_SUCCESS:
            return Object.assign(state, {
                isAxiosing: false,
                info:action.info,
                msg:action.msg
            });
        case AJAX_ERROR:
            return Object.assign(state, {
                isAxiosing: false,
                msg:action.msg
            })
    }
};


//--------------------
//Mutations
//--------------------
export const mutations = {
    [type.GETUSERINFO_POST](state,action) {
       state.user= Object.assign({},state.user,getLoinInfoPostMutationHandle(state.user,action))
    },
    [type.SETVISITPATH](state,action){
         state.visit = Object.assign({},state.visit,{path:action})
    }

};




